package com.gitee.wsl.compose.ui.section.lazylist.item

import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Divider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import com.gitee.wsl.compose.ext.ui.copy
import com.gitee.wsl.compose.ui.section.CupertinoSectionDefaults
import com.gitee.wsl.compose.ui.section.CupertinoSectionTokens
import com.gitee.wsl.compose.ui.section.lazylist.LazySectionScope

fun LazySectionScope.expandableRow(
    key: Any?,
    contentType: Any?,
    dividerPadding: Dp,
    modifier : @Composable () -> Modifier = { Modifier },
    title: @Composable () -> Unit,
    belowContentExpanded : Boolean,
    belowContent : @Composable () -> Unit,
    endContent : @Composable () -> Unit
) = item(
    key = key,
    contentType = contentType,
    dividerPadding = dividerPadding
) { padding ->

    var expandedBeforeAnimation by rememberSaveable {
        mutableStateOf(belowContentExpanded)
    }

    Column {
        Row(
            modifier = modifier()
                .fillMaxWidth()
                .heightIn(min = CupertinoSectionTokens.MinHeight)
                .padding(
                    padding.copy(
                        top = padding.calculateTopPadding() / 2,
                        bottom = padding.calculateBottomPadding() / 2
                    )
                ),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            title()
            endContent()
        }
        if (belowContentExpanded || expandedBeforeAnimation) {
            Divider(
                modifier = Modifier
                    .padding(start = CupertinoSectionDefaults.DividerPadding)
            )
        }
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .animateContentSize(
                    animationSpec = tween(),
                    finishedListener = { _, _ ->
                        expandedBeforeAnimation = belowContentExpanded
                    }
                )
        ) {
            if (belowContentExpanded) {
                Box(
                    Modifier.padding(
                        top = padding.calculateTopPadding(),
                        bottom = padding.calculateBottomPadding()
                    )
                ) {
                    belowContent()
                }
            }
        }
    }
}